//
//  ScreeningViewController.m
//  HTP
//
//  Created by admin on 15/11/1.
//  Copyright © 2015年 nit. All rights reserved.
//

#import "ScreeningViewController.h"
#import "GroupButton.h"
#define UIIMGARVIEWPADDING 25   
#define UILABELPADDING 10
#define UILABELHEIGHT 15
#define VIEWHEIGHT  (ScreenWidth - 30) / 4.0
typedef NS_ENUM(NSInteger,SortType) {
    defaultType = 0,
    timeType    = 1,
    goodType    = 2,
    countType   = 3,
    lengthType  = 4
};
@interface ScreeningViewController ()<GroupButtonDegeta>
@property (nonatomic,retain) UIScrollView   *mainScrollView;
@property (nonatomic,strong) UIButton       *areaBtn;
@property (nonatomic,strong) UIButton       *gradeBtn;
@property (nonatomic,retain) GroupButton    *allSex;
@property (nonatomic,retain) GroupButton    *maleBtn;
@property (nonatomic,retain) GroupButton    *fmaleBtn;
@property (nonatomic,retain) GroupButton    *allStudy;
@property (nonatomic,strong) GroupButton    *studyOne;
@property (nonatomic,retain) GroupButton    *studyMore;
@property (nonatomic,retain) GroupButton    *teacherZBtn;
@property (nonatomic,retain) GroupButton    *unZBtn;

@property (nonatomic,assign) CGFloat         viewY;

@property (nonatomic,retain) NSMutableArray *btns;
@end

@implementation ScreeningViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view addSubview:self.mainScrollView];
    [self _initView];
    // Do any additional setup after loading the view.
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self addCustomBackWithAction:@selector(backSelectTeacher) normalImg:SYSTEMBACKIMAGE selectImg:SYSTEMBACKIMAGE withPadding:8];
    [self addCustomTitleWithTitle:@"筛选条件"];
    [self addCustomRightWithAction:@selector(sureClick) title:@"确定" normalImg:nil selectImg:nil];
}
//TODO:重写get方法
-(UIScrollView *)mainScrollView{
    if (!_mainScrollView) {
        _mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeihgt - NavHe)];
        _mainScrollView.backgroundColor = RGBCOLOR(243, 243, 243, 1);
        _mainScrollView.userInteractionEnabled = YES;
    }
    return _mainScrollView;
}
//TODO:绘制UI
-(void)_initView{
    [self areaView];
    [self sexyView];
    [self studyView];
    [self sureView];
    [self sortMedthod];
}
-(void)areaView{
    //start 30 height 45
    UIImageView *areaImage = [[UIImageView alloc] initWithFrame:CGRectMake(15, 20, ScreenWidth - 30, VIEWHEIGHT/3.0*2.0)];
    areaImage.image        = [UIImage imageNamed:@"normal"];
    [self.mainScrollView addSubview:areaImage];
    _viewY = CGRectGetMaxY(areaImage.frame);
    
    UIImageView *lineView = [[UIImageView alloc] initWithFrame:CGRectMake((ScreenWidth - 30) / 2.0, 10, 1, VIEWHEIGHT/3.0*2.0-20)];
    lineView.image        = [UIImage imageNamed:@"line_nianji"];
    [areaImage addSubview:lineView];
    
    UILabel *cityLable = [[UILabel alloc] init];
    cityLable.text = @"城市";
    cityLable.font = [UIFont systemFontOfSize:15];
    cityLable.textColor = RGBCOLOR(0, 0, 0, 0.7);
    [areaImage addSubview:cityLable];
    [cityLable makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(areaImage);
        make.left.equalTo(areaImage).offset(15);
    }];
    
    _areaBtn = [[UIButton alloc] init];
    [_areaBtn setTitle:@"宁波市" forState:UIControlStateNormal];
    [_areaBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [_areaBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [_areaBtn setTintColor:[UIColor orangeColor]];
    [areaImage addSubview:_areaBtn];
    [_areaBtn makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(areaImage);
        make.left.equalTo(cityLable.right).offset(10);
    }];
    
    UIImageView *moreImg = [[UIImageView alloc] init];
    moreImg.contentMode  = UIViewContentModeScaleAspectFit;
    [moreImg setImage:[UIImage imageNamed:@"more"]];
    [areaImage addSubview:moreImg];
    [moreImg makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(areaImage);
        make.left.equalTo(_areaBtn.right).offset(10);
    }];
    
    UILabel *gradeLabel = [[UILabel alloc] init];
    gradeLabel.text = @"年纪";
    gradeLabel.font = [UIFont systemFontOfSize:15];
    gradeLabel.textColor = RGBCOLOR(0, 0, 0, 0.7);
    [areaImage addSubview:gradeLabel];
    [gradeLabel makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(areaImage);
        make.left.equalTo(lineView).offset(25);
    }];
    
    _gradeBtn = [[UIButton alloc] init];
    [_gradeBtn setTitle:@"一年级" forState:UIControlStateNormal];
    [_gradeBtn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    [_gradeBtn.titleLabel setFont:[UIFont systemFontOfSize:15]];
    [_gradeBtn setTintColor:[UIColor orangeColor]];
    [areaImage addSubview:_gradeBtn];
    [_gradeBtn makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(areaImage);
        make.left.equalTo(gradeLabel.right).offset(10);
    }];
    
    UIImageView *moreImg_1 = [[UIImageView alloc] init];
    moreImg_1.contentMode  = UIViewContentModeScaleAspectFit;
    [moreImg_1 setImage:[UIImage imageNamed:@"more"]];
    [areaImage addSubview:moreImg_1];
    [moreImg_1 makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(areaImage);
        make.left.equalTo(_gradeBtn.right).offset(10);
    }];
}
-(void)sexyView{
    //start 90 75
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, _viewY + 20, ScreenWidth, UILABELHEIGHT)];
    label.text = @"性别";
    label.textAlignment = _TEXT_ALIGN_CENTER;
    label.font = [UIFont fontWithName:@"Helvetica-BoldOblique" size:16];
    label.textColor = RGBCOLOR(0, 0, 0, 0.7);
    _viewY = CGRectGetMaxY(label.frame);
    [self.mainScrollView addSubview:label];
    
    UIImageView *areaImage = [[UIImageView alloc] initWithFrame:CGRectMake(15, _viewY + 10, ScreenWidth - 30, VIEWHEIGHT)];
    areaImage.image        = [UIImage imageNamed:@"normal"];
    areaImage.userInteractionEnabled = YES;
    [self.mainScrollView addSubview:areaImage];
    _viewY = CGRectGetMaxY(areaImage.frame);
    
    _allSex = [[GroupButton alloc] initWithDelegate:self groupId:@"sexy" buttonImage:@"sex_1" unSelectImage:@"usex_1" title:@"全部" withFrame:CGRectMake(10, 10, (VIEWHEIGHT - 10)/3.0*2.0, VIEWHEIGHT - 20)];
    [areaImage addSubview:_allSex];
    
    _maleBtn = [[GroupButton alloc] initWithDelegate:self groupId:@"sexy" buttonImage:@"sex_3" unSelectImage:@"usex_3" title:@"男老师" withFrame:CGRectMake(ScreenWidth-30-(VIEWHEIGHT -10)/ 3.0 * 2.0 -10, 10, (VIEWHEIGHT - 10)/3.0*2.0, VIEWHEIGHT - 20)];
    NSLogFrame(_maleBtn.frame);
    [areaImage addSubview:_maleBtn];
    
    _fmaleBtn = [[GroupButton alloc] initWithDelegate:self groupId:@"sexy" buttonImage:@"sex_2" unSelectImage:@"usex_2" title:@"女老师" withFrame:CGRectMake((ScreenWidth - 30 -VIEWHEIGHT / 3.0 * 2.0)/2.0, 10, (VIEWHEIGHT - 10)/3.0*2.0, VIEWHEIGHT - 20)];
    [areaImage addSubview:_fmaleBtn];
    
    _allSex.isSelect = YES;
    
    for (int i = 1; i <= 2; i++) {
        UIImageView *lineView = [[UIImageView alloc] initWithFrame:CGRectMake((ScreenWidth - 30) / 3.0 *i, 15, 1, VIEWHEIGHT-30)];
        lineView.image        = [UIImage imageNamed:@"line_nianji"];
        [areaImage addSubview:lineView];
    }
    
}
-(void)studyView{
    //start 90 height 75
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, _viewY + 20, ScreenWidth, UILABELHEIGHT)];
    _viewY = CGRectGetMaxY(label.frame);
    label.text = @"授课方式";
    label.textAlignment = _TEXT_ALIGN_CENTER;
    label.textColor = RGBCOLOR(0, 0, 0, 0.7);
    label.font = [UIFont fontWithName:@"Helvetica-BoldOblique" size:16];
    [self.mainScrollView addSubview:label];
    
    UIImageView *areaImage = [[UIImageView alloc] initWithFrame:CGRectMake(15, _viewY + 10, ScreenWidth - 30, VIEWHEIGHT)];
    areaImage.image        = [UIImage imageNamed:@"normal"];
    areaImage.userInteractionEnabled = YES;
    [self.mainScrollView addSubview:areaImage];
    _viewY = CGRectGetMaxY(areaImage.frame);
    
    _allStudy = [[GroupButton alloc] initWithDelegate:self groupId:@"study" buttonImage:@"all" unSelectImage:@"uall" title:@"全部" withFrame:CGRectMake(10, 10, (VIEWHEIGHT - 10)/3.0*2.0, VIEWHEIGHT - 20)];
    [areaImage addSubview:_allStudy];
    
     _studyMore = [[GroupButton alloc] initWithDelegate:self groupId:@"study" buttonImage:@"stu_2" unSelectImage:@"ustu_2" title:@"班课" withFrame:CGRectMake(ScreenWidth-30-(VIEWHEIGHT -10)/ 3.0 * 2.0 -10, 10, (VIEWHEIGHT - 10)/3.0*2.0, VIEWHEIGHT - 20)];
    NSLogFrame(_maleBtn.frame);
    [areaImage addSubview:_studyMore];
    
    _studyOne = [[GroupButton alloc] initWithDelegate:self groupId:@"study" buttonImage:@"stu_1" unSelectImage:@"ustu_1" title:@"1对1教学" withFrame:CGRectMake((ScreenWidth - 30 -VIEWHEIGHT)/2.0, 10, VIEWHEIGHT, VIEWHEIGHT - 20)];
    [areaImage addSubview:_studyOne];
    
    _allStudy.isSelect = YES;
    
    for (int i = 1; i <= 2; i++) {
        UIImageView *lineView = [[UIImageView alloc] initWithFrame:CGRectMake((ScreenWidth - 30) / 3.0 *i, 15, 1, VIEWHEIGHT-30)];
        lineView.image        = [UIImage imageNamed:@"line_nianji"];
        [areaImage addSubview:lineView];
    }

}
-(void)sureView{
    //start 90 height 75
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, _viewY + 20, ScreenWidth, UILABELHEIGHT)];
    label.text = @"认证";
    label.textAlignment = _TEXT_ALIGN_CENTER;
    label.textColor = RGBCOLOR(0, 0, 0, 0.7);
    label.font = [UIFont fontWithName:@"Helvetica-BoldOblique" size:16];
     _viewY = CGRectGetMaxY(label.frame);
    [self.mainScrollView addSubview:label];
    
    UIImageView *areaImage = [[UIImageView alloc] initWithFrame:CGRectMake(15, _viewY + 10, ScreenWidth - 30, VIEWHEIGHT)];
    areaImage.image        = [UIImage imageNamed:@"normal"];
    areaImage.userInteractionEnabled = YES;
    [self.mainScrollView addSubview:areaImage];
    
    _viewY = CGRectGetMaxY(areaImage.frame);
    _teacherZBtn = [[GroupButton alloc] initWithDelegate:self groupId:@"teacher" buttonImage:@"ju_zhengjian" unSelectImage:@"zhengjian" title:@"持证教师" withFrame:CGRectMake((ScreenWidth - 30) / 2.0 + 30, 10, VIEWHEIGHT, VIEWHEIGHT - 20)];
    
     _unZBtn = [[GroupButton alloc] initWithDelegate:self groupId:@"teacher" buttonImage:@"all" unSelectImage:@"uall" title:@"全部" withFrame:CGRectMake((ScreenWidth - 30) / 2.0 - VIEWHEIGHT - 30, 10, VIEWHEIGHT, VIEWHEIGHT - 20)];
    
    _unZBtn.isSelect = YES;
    [areaImage addSubview:_unZBtn];
    [areaImage addSubview:_teacherZBtn];
    
    UIImageView *lineView = [[UIImageView alloc] initWithFrame:CGRectMake((ScreenWidth - 30) / 2.0, 15, 1, VIEWHEIGHT-30)];
    lineView.image        = [UIImage imageNamed:@"line_nianji"];
    [areaImage addSubview:lineView];

}
-(void)sortMedthod{
    //start 90 height 75
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, _viewY + 20, ScreenWidth, UILABELHEIGHT)];
    label.text = @"排序方式";
    label.textAlignment = _TEXT_ALIGN_CENTER;
    label.textColor = RGBCOLOR(0, 0, 0, 0.7);
    label.font = [UIFont fontWithName:@"Helvetica-BoldOblique" size:16];
    _viewY = CGRectGetMaxY(label.frame);
    [self.mainScrollView addSubview:label];
    
    UIImageView *areaImage = [[UIImageView alloc] initWithFrame:CGRectMake(15, _viewY + 10, ScreenWidth - 30, VIEWHEIGHT*1.8)];
    areaImage.image        = [[UIImage imageNamed:@"normal"] stretchableImageWithLeftCapWidth:10 topCapHeight:10];
    areaImage.userInteractionEnabled = YES;
    [self.mainScrollView addSubview:areaImage];
    
    _viewY = CGRectGetMaxY(areaImage.frame)+10;
    
    [self.mainScrollView setContentSize:CGSizeMake(ScreenWidth, _viewY)];
    
    UIButton *btn1 = [[UIButton alloc] init];
    [btn1 setTitle:@"默认排序" forState:UIControlStateNormal];
    [btn1 setTitle:@"默认排序" forState:UIControlStateSelected];
    [btn1 setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
    [btn1 setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    btn1.selected = YES;
    [areaImage addSubview:btn1];
    [btn1 makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(areaImage.top).offset(10);
        make.centerX.equalTo (areaImage);
        make.height.equalTo(20);
    }];
    btn1.tag = defaultType;
    UIButton *btn_c = [[UIButton alloc] init];
    [btn_c setTitle:@"学生数从多到少" forState:UIControlStateNormal];
    [btn_c setTitle:@"学生数从多到少" forState:UIControlStateSelected];
    [btn_c setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
    [btn_c setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [areaImage addSubview:btn_c];
    [btn_c makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(areaImage).offset(10);
        make.bottom.equalTo(areaImage).offset(-10);
    }];
    btn_c.tag = countType;
    UIButton *btn_l = [[UIButton alloc] init];
    [btn_l setTitle:@"距离有远到近" forState:UIControlStateNormal];
    [btn_l setTitle:@"距离有远到近" forState:UIControlStateSelected];
    [btn_l setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
    [btn_l setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [areaImage addSubview:btn_l];
    [btn_l makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(areaImage).offset(-10);
        make.bottom.equalTo(areaImage).offset(-10);
    }];
    btn_l.tag = lengthType;
    UIButton *btn_t = [[UIButton alloc] init];
    [btn_t setTitle:@"课时量从高到低" forState:UIControlStateNormal];
    [btn_t setTitle:@"课时量从高到低" forState:UIControlStateSelected];
    [btn_t setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
    [btn_t setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [areaImage addSubview:btn_t];
    [btn_t makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(btn_c);
        make.bottom.equalTo(btn_c).offset(-40);
    }];
    btn_t.tag = timeType;
    UIButton *btn_g = [[UIButton alloc] init];
    [btn_g setTitle:@"评分从高到低" forState:UIControlStateNormal];
    [btn_g setTitle:@"评分从高到低" forState:UIControlStateSelected];
    [btn_g setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
    [btn_g setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [areaImage addSubview:btn_g];
    [btn_g makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(areaImage).offset(-10);
        make.bottom.equalTo(btn_l).offset(-40);
    }];
    btn_g.tag = goodType;
    
    _btns = [NSMutableArray arrayWithObjects:btn1,btn_c,btn_g,btn_l,btn_t ,nil];
    [_btns enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *btn = (UIButton *)obj;
        [btn addTarget:self action:@selector(didSeleSortMethod:) forControlEvents:UIControlEventTouchUpInside];
        [btn.titleLabel setFont:[UIFont systemFontOfSize:15]];
    }];
    
    
    UIImageView *line_s = [[UIImageView alloc] init];
    line_s.image = [UIImage imageNamed:@"line_paixu1"];
    [areaImage addSubview:line_s];
    [line_s makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(areaImage);
        make.top.equalTo(btn_t.top).offset(-5);
        make.bottom.equalTo(areaImage.bottom);
    }];
    
    UIImageView *line_h = [[UIImageView alloc] init];
    line_h.image = [UIImage imageNamed:@"line_paixu"];
    [areaImage addSubview:line_h];
    [line_h makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(areaImage);
        make.bottom.equalTo(btn_t).offset(10);
        make.width.equalTo(areaImage);
        make.height.equalTo(1);
    }];
    
    UIImageView *line_h_1 = [[UIImageView alloc] init];
    line_h_1.image = [UIImage imageNamed:@"line_paixu"];
    [areaImage addSubview:line_h_1];
    [line_h_1 makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(areaImage);
        make.top.equalTo(line_s.top);
        make.width.equalTo(areaImage);
        make.height.equalTo(1);
    }];
    
}
//TODO:groupbutton delegate
-(void)didSelectGroup:(GroupButton *)groupBtn groupId:(NSString *)groupId{
    DLog(@"%@",groupBtn);
}
//TODO:ACTION
-(void)backSelectTeacher{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)didSeleSortMethod:(id)sender{
    NSInteger tag = -1;
    UIButton *btn = (UIButton *)sender;
    tag = btn.tag;
    [_btns enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *btnSelect = (UIButton *)obj;
        if (btnSelect.tag == tag) {
            btnSelect.selected = YES;
        }else{
            btnSelect.selected = NO;
        }
    }];
}
-(void)sureClick{
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
